<?php
/**
 * 提现列表
 * author itholiday@126.com
 */
global $_W, $_GPC;
load()->func('tpl');
$html_title = $_W['current_module']['title'];
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'list';
//页面显示条数
$pagesize = 20;
$page = max(1, $_GPC['page']);
$start = ($page - 1) * $pagesize;

$where = "A.id>0";

$user_id = isset($_GPC['user_id']) && !empty($_GPC['user_id']) ? input('user_id') : '';
$logtype = isset($_GPC['logtype']) ? input('logtype') : '';
$start_time = isset($_GPC['start_time']) && !empty($_GPC['start_time']) ? strtotime($_GPC['start_time']) : '';
$end_time = isset($_GPC['end_time']) && !empty($_GPC['end_time']) ? strtotime($_GPC['end_time']) : '';

if (!empty($user_id)){
    $where .= " and (A.user_id = '$user_id' or A.user_mobile='$user_id')";
}

if (!empty($logtype) || $logtype == "0"){
    $where .= " and A.cash_status='$logtype' ";
}

if (!empty($start_time) && !empty($end_time)) {
    $where .= " and A.create_time>=$start_time and A.create_time<=$end_time ";
} else {
    if (!empty($start_time)) {
        $where .= " and A.create_time>=$start_time ";
    }
    if (!empty($end_time)) {
        $where .= " and A.create_time<=$end_time ";
    }
}

//if ($op == 'review') {
//    //单条审核
//    if (isset($_GPC['id'])) {
//        $id = intval($_GPC['id']);
//        $yes = $_GPC['yes'] ? 1 : 0;
//        $review_mark = isset($_GPC['review_mark']) ? trim($_GPC['review_mark']) : '';
//        $receive_order = pdo_get('yq_haircircle_task_receive', array('id' => $id));
//        //判断审核状态
//        if ($receive_order['receive_status'] < 2) {
//            ajaxError('该订单正在进行或已失效，不可审核');
//        }
//        if ($receive_order['receive_status'] == 3 || $receive_order['review_status'] > 0) {
//            ajaxError('该订单已处理过');
//        }
//        reviewTask($receive_order, $yes, $review_mark);
//        ajaxSuccess('审核完成');
//    }
//    //批量审核
//    if (isset($_GPC['ids'])) {
//        $ids = $_GPC['ids'];
//        $yes = $_GPC['yes'] ? 1 : 0;
//        foreach ($ids as $id) {
//            $review_mark = isset($_GPC['review_mark']) ? trim($_GPC['review_mark']) : '';
//            $receive_order = pdo_get('yq_haircircle_task_receive', array('id' => $id));
//            //判断审核状态
//            if ($receive_order['receive_status'] < 2) {
//                ajaxError('所选订单ID' . $receive_order['id'] . '正在进行或已失效，不可审核');
//            }
//            if ($receive_order['receive_status'] == 3 || $receive_order['review_status'] > 0) {
//                ajaxError('订单ID' . $receive_order['id'] . '已处理过');
//            }
//            reviewTask($receive_order, $yes, $review_mark);
//        }
//        ajaxSuccess('审核完成');
//    }
//}

function cashOut($cashout, $yes = 0, $notes = '')
{
    //对应的用户信息
    $user = pdo_get('yq_haircircle_users',array('id'=>$cashout['user_id']));
    pdo_begin();
    try{
        if ( $yes == 1){
            $data['handle_id'] = $_W['uid']; //处理人id
            $data['handle_time'] = time();
            $data['cash_status'] = 1;

            //修改提现状态
            $res = pdo_update('yq_haircircle_cashout',$data,array('id' => $cashout['id']));
            if (empty($res)) {
                pdo_rollback();
                ajaxError('修改审核状态失败');
            }

            pdo_commit();
            return true;
        } else {
            // 驳回
            if (empty($notes)){
                ajaxError('请填写驳回理由');
            }

            $cashoutLog = [
                'oid' => $cashout['id'],
                'user_id' => $cashout['user_id'],
                'type' => 4,
                'change' => $cashout['cash_money'] + $cashout['tx_fee'],
                'mobile' => $user['mobile'],
                'balance' => $user['user_money'] + $cashout['cash_money'] + $cashout['tx_fee'],
                'remark' => '提现驳回:'.$notes,
                'create_time' => time()
            ];

            $res = pdo_insert('yq_haircircle_user_money_log',$cashoutLog);
            if (empty($res)){
                ajaxError('添加日志失败');
            }

            $cashoutData = [
                'handle_id' => $_W['uid'],
                'handle_time' => time(),
                'cash_status' => -1,
                'cash_mark' => $notes
            ];
            $res = pdo_update('yq_haircircle_cashout',$cashoutData,array('id'=>$cashout['id']));
            if (empty($res)){
                ajaxError('驳回失败');
            }
            pdo_commit();

            return true;
        }

    }catch (\Exception $e) {
        pdo_rollback();
        ajaxError('系统出现异常:' . $e->getMessage());
    }

}

if ($op == 'cashout'){
    //单条审核
    if (isset($_GPC['id'])){
        $id = intval($_GPC['id']);
        $yes = $_GPC['yes'] ? 1 : 0;
        $cash_mark = isset($_GPC['cash_mark']) ? trim($_GPC['cash_mark']) : '';

        $cash = pdo_get('yq_haircircle_cashout',array('id'=>$id));

        if ($cash['cash_status'] == 1){
            ajaxError('提现已通过');
        }
        if ($cash['cash_status'] != 0){
            ajaxError('提现操作已经被处理');
        }
        cashOut($cash, $yes, $cash_mark);
        ajaxSuccess('审核完成');
    }
    //多条审核
    if (isset($_GPC['ids'])) {
        $ids = $_GPC['ids'];
        $yes = $_GPC['yes'] ? 1 : 0;
        foreach ($ids as $id) {
            $cash_mark = isset($_GPC['cash_mark']) ? trim($_GPC['cash_mark']) : '';
            $cash = pdo_get('yq_haircircle_cashout', array('id' => $id));
            //判断审核状态
            if ($cash['cash_status'] == 1){
                ajaxError('提现已通过');
            }
            if ($cash['cash_status'] != 0){
                ajaxError('提现操作已经被处理');
            }
            cashOut($cash, $yes, $cash_mark);
        }
        ajaxSuccess('审核完成');
    }

}

if ($op == 'list' || $op == '') {

    $pagediv = 'list';
    $total = pdo_fetchcolumn('SELECT COUNT(*) AS `number` FROM ' .tablename('yq_haircircle_cashout') . ' A WHERE ' . $where);
    $pager = getPageHTML($total,$page,$pagesize);
    $sql = "select A.* from ims_yq_haircircle_cashout A  where $where order by A.id desc limit $start,$pagesize";
    $list = pdo_fetchall($sql);
    include $this->template('web/cashout_list');
    exit;
}